//API 操作
const API = require('../../datas/common.js');

//创建精选页面对象
Page({
    data: {
        version: 'beta 1.0.0',
        storageSize: 0,
        sex: ['男', '女'],
        editNickname: false,
        inputNickname: '',
        hometown: ['武穴街道', '刊江街道', '田镇街道', '万丈湖街道', '梅川镇', '余川镇', '花桥镇', '石佛寺镇', '大金镇', '四望镇', '大法寺镇', '龙坪镇'],
        userDetail: null,
        sexIndex: 0,
        hometownIndex: 0
    },
    onLoad: function () {
        this.getUserInfo();
    },
    onShow: function () {

    },
    onReady: function () {
        if (null == this.data.userDetail) {
            this.getUserInfo();
        }
    },
    getUserInfo: function () {
        let that = this;
        API.showLoading('加载中...');
        API.getUserInfo().then(res => {
            that.setData({
                storageSize: that.getStorageSize(),
                userDetail: res,
                sexIndex: res.gender == 'MALE' ? 0 : 1,
                hometownIndex: that.data.hometown.findIndex(function (element) {
                    return element == res.hometown;
                })
            })
            API.hideLoading();
        }).catch((res) => {
            API.showToast(res.message);
        })
    },
    getStorageSize: function () {
        var storageSize;
        try {
            var storage = wx.getStorageInfoSync()
            storageSize = storage.currentSize / 1024 >= 1 ? (storage.currentSize / 1024).toFixed(2) + 'MB' : storage.currentSize + 'KB';
        } catch (e) {
            storageSize = '获取失败';
        }
        return storageSize;
    },
    changeSex: function (e) {
        let sex = this.data.sex[e.detail.value] == '男' ? 'MALE' : 'FEMALE';
        let userInfo = {gender: sex};
        this.updateUser(userInfo);
    },
    changeHometown: function (e) {
        let hometown = this.data.hometown[e.detail.value];
        let userInfo = {hometown: hometown};
        this.updateUser(userInfo);
    },
    changeShowHometown: function (e) {
        let showHometown = e.detail.value;
        let userInfo = {showHometown: showHometown};
        this.updateUser(userInfo);
    },
    updateUser: function (userInfo) {
        let that = this;
        API.showLoading('更新中...');
        API.put('/user', userInfo).then(res => {
            let userInfo = res.data;
            that.setData({
                userDetail: userInfo,
                sexIndex: userInfo.gender == 'MALE' ? 0 : 1,
                hometownIndex: that.data.hometown.findIndex(function (element) {
                    return element == userInfo.hometown;
                })
            });
            if (that.data.editNickname) {
                that.setData({
                    editNickname: false,
                    inputNickname: '',
                });
            }
            getApp().globalData.userInfo = userInfo;
            API.setStorage('wuxuegai_userInfo', userInfo);
            API.hideLoading();
        }).catch((res) => {
            if (that.data.editNickname) {
                that.setData({
                    editNickname: false,
                    inputNickname: '',
                });
            }
            API.showToast(res.message);
        });
    },
    clearStorage: function () {
        let that = this;
        if (!that.data.editNickname) {
            wx.showActionSheet({
                itemList: ['清除缓存'],
                success: function (res) {
                    if (res.tapIndex == 0) {
                        API.showLoading('清除中...')
                        wx.clearStorageSync();
                        that.setData({
                            storageSize: that.getStorageSize()
                        });
                        API.hideLoading();
                    }
                }
            })
        }
    },
    editNickname: function () {
        this.setData({
            editNickname: true
        })
    },
    changeUsername: function (e) {
        let that = this;
        let nickname = e.detail.value;
        if (!nickname) {
            that.setData({
                editNickname: false,
            });
        }else {
            wx.showActionSheet({
                itemList: ['保存'],
                success: function (res) {
                    if (res.tapIndex == 0) {
                        let userInfo = {nickname: nickname};
                        that.updateUser(userInfo);
                    } else {
                        that.setData({
                            editNickname: false,
                            inputNickname: '',
                        });
                    }
                }
            })
        }
    }
});